$(function() {

  var id = $.unparam(location.search.substring(1)).id;

  function getDayChinese(day) {
    var dayChinese = null;
    switch (day) {
      case 0:
        dayChinese = '日';
        break;
      case 1:
        dayChinese = '一';
        break;
      case 2:
        dayChinese = '二';
        break;
      case 3:
        dayChinese = '三';
        break;
      case 4:
        dayChinese = '四';
        break;
      case 5:
        dayChinese = '五';
        break;
      case 6:
        dayChinese = '六';
        break;
    }

    return dayChinese;
  };

  function zerofill(n) {
    return n < 10 ? '0' + n : n;
  }

  $.ajax({
    url: '/qfang-weixin/qiye/meeting/select',
    data: { id: id },
    beforeSend: function() {
      $.showPreloader();
    },

    success: function(data) {
      if (data.code == '1000') {
        $.each(data.data, function(k, v) {
          if (k == 'useDate') {
            var newDate = new Date(v);
            $('#useDate').val('周' + getDayChinese(newDate.getDay()) + '  (' + zerofill(newDate.getMonth() + 1) + '月' + zerofill(newDate.getDate()) + ')');
          } else if (k == 'conferenceName') {
            $('#meetingName').val(v);
          } else if (k == 'dates') {
            $('#meetingDate').val('' + v.split(',').length + '小时');
          }
        });
      }
    },

    complete: function() {
      $.hidePreloader();
    }
  });

  var search = $('#search');
  var personnelList = $('#personnelList');
  var personelContent = $('#personelContent');
  var personArry = [];

  personelContent.on('click', 'dt,input', function() {
    $.popup('#searchPopup');
    $('#personNumber').text(personArry.length);
    personnelList.empty();
  });

  search.on('keyup', function() {
    var keyword = $.trim($(this).val());
    setTimeout(function() {
      if (keyword) {
        searPerson(keyword);
      }
    }, 100);
  });

  $('#searchBtn').on('click', function() {
    var keyword = $.trim(search.val());
    if (keyword) {
      searPerson(keyword);
    }
  });

  function searPerson(keyword) {
    $.ajax({
      url: '/qfang-weixin/qiye/contactsList/listByKeyword',
      data: {
        keyword: keyword,
        mobile: $.config.mobile
      },
      success: function(data) {
        if (data.code == '1000') {
          var html = '';
          var className = '';
          var classImg = '';
          if (data.data.items.length) {
            $.each(data.data.items, function(k, v) {
              if (v.photoUrl) {
                className = 'dn';
              } else {
                classImg = 'dn';
              }

              html += '<li data-id="' + v.id + '" data-name="' + v.personName + '">' +
                '<div class="select-person"><span class="icon icon-check dn"></span></div>' +
                '<span class="person-name">' + v.personName + '</span> (<span class="organization">' + v.orgName + '</span>) ' +
                '</li>';
            });

            personnelList.empty().append(html);
            $('.person-nav').css({
              position: 'fixed',
              bottom: 0
            });

            $('.no-data').hide();
          } else {
            personnelList.empty();
            $('.no-data').show();
          }
        }
      }
    });
  }

  personnelList.on('click', 'li', function() {
    var $iconCheck = $(this).find('.icon-check');
    var $selectPerson = $(this).find('.select-person');
    var personnelId = $(this).data('id');
    var personnelName = $(this).data('name');
    var flag = false;

    if ($iconCheck.is(':visible')) {
      $iconCheck.addClass('dn');
      $selectPerson.removeClass('active');
      for (var i = 0; i < personArry.length; i++) {
        if (personArry[i].id == personnelId) {
          personArry.splice(i, 1);
          break;
        }
      }
    } else {
      for (var i = 0; i < personArry.length; i++) {
        if (personArry[i].id == personnelId) {
          $.toast('' + personnelName + '已添加');
          flag = true;
          break;
        }
      }

      if (flag) {
        return false;
      }

      $iconCheck.removeClass('dn');
      $selectPerson.addClass('active');
      personArry.push({ id: personnelId, name: personnelName });
    }

    $('#personNumber').text(personArry.length);
  });

  $('#carryOut').on('click', function() {
    $.closeModal('#searchPopup');
    search.val('');
    personelContent.empty();

    if (personArry.length) {
      var html = '<dl id="personnelDl"><dt><p>添加会议成员</p><span class="iconfont icon-tianjia"></span></dt>';
      $.each(personArry, function(k, v) {
        html += '<dd data-id="' + v.id + '"><p><span class="number">' + (k + 1) + '</span> ' + v.name + '</p><span class="delete iconfont icon-shanchu"></span></dd>';
      });

      html += '</dl>';
      personelContent.empty().append(html);
    } else {
      personelContent.empty().append('<input type="text" id="useDate" name="useDate" placeholder="请输入会议主题">');
    }

  });

  $('#personelContent').on('click', '.delete', function() {
    var selfDd = $(this).parents('dd');
    var personnelId = selfDd.data('id');
    var personnelDl = $('#personnelDl');
    selfDd.hide(1000).remove();
    for (var i = 0; i < personArry.length; i++) {
      if (personArry[i].id == personnelId) {
        personArry.splice(i, 1);
        break;
      }
    }

    for (var i = 0; i < personArry.length; i++) {
      personnelDl.find('dd>p>.number').eq(i).text(i + 1);
    }
  });

  $('#submit').on('click', function() {
    var flag = false;
    if (!personArry.length) {
      $.toast('请添加会议成员');
      flag = true;
    } else if (!$('#conferenceTheme').val()) {
      $.toast('请输入会议主题');
      flag = true;
    }

    if (flag) {
      return false;
    }

    var personIdArry = [];
    $.each(personArry, function(k, v) {
      personIdArry.push(v.id);
    });

    var param = {
      personIds: personIdArry.join(','),
      title: $('#conferenceTheme').val(),
      id: id
    };

    $.ajax({
      url: '/qfang-weixin/qiye/meeting/update',
      type: 'post',
      data: param,
      beforeSend: function() {
        $('#submit').prop('disabled', true);
        $.showPreloader();
      },

      success: function(data) {
        if (data.code == '1000') {
          $.toast('添加成功');
          setTimeout(function() {
            location.href = 'my-booking.html';
          }, 1000);
        } else {
          $.toast(data.msg);
          $('#submit').prop('disabled', false);
        }
      },

      complete: function() {
        $.hidePreloader();
      }
    });
  });

  history.replaceState(null, null, 'my-booking.html');
});
